package logic

import (
	"context"
	"fmt"
	"person/models/mysql"
	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectLogic) Perfect(in *personrpc.PerfectRequest) (*personrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var userFind mysql.User
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&userFind)
	if userFind.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在，禁止完善信息")
	}
	user := mysql.User{
		RealName: in.RealName,
		Idcard:   in.Idcard,
		Sex:      in.Sex,
		Birthday: in.Birthday,
	}
	mysql.DB.Debug().Where("id = ?", in.Id).Updates(&user)

	return &personrpc.PerfectResponse{
		Id: int64(userFind.ID),
	}, nil
}
